package 实验四;

class SharedResource {
    private int sharedValue = 0; 
    public synchronized void increment() {
        sharedValue++;
        System.out.println("Incremented: " + sharedValue);
    }
    public synchronized void printValue() {
        System.out.println("Current Value: " + sharedValue);
    }
    public static void main(String[] args) {
        SharedResource resource = new SharedResource(); 
        Thread incrementThread = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                resource.increment(); 
            }
        });
        Thread printThread = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                resource.printValue(); 
            }
        });
        incrementThread.start();
        printThread.start();
    }
}


